﻿using System;

namespace Library.WebControls.SimpleGridView {
	public class SpGvRowEditedEventArgs : CustomCallbackEventArgs {
		private readonly string _calbackData;
		private bool _isParse;
		internal SpGvRowEditedEventArgs(string parameter, string callbackData, string key)
			: base(parameter) {
			_calbackData = callbackData;
			Key = key;
		}

		private void Parse() {
			if (_isParse) return;
			_isParse = true;
			if (String.IsNullOrEmpty(_calbackData)) return;
			var ps = BaseControl.FromArrayString(_calbackData);
			for (int i = 0; i < ps.Length; i++) {
				var name = ps[i];
				i++;
				if (i < ps.Length) {
					var value = ps[i];

					SpGvBaseColumn column = Grid.VisibleColumns.Find(name);
					if (column != null) {
						column.EditValuePostedFromClientOnEditMode = value;
					}
				}

			}
			
		}
		public string Key { get; internal set; }

		public object GetColumnEditValue(string columnName) {
			Parse();
			if (String.IsNullOrEmpty(columnName))
				throw new ArgumentNullException("columnName");
			SpGvBaseColumn column = Grid.VisibleColumns.Find(columnName);
			if (column == null) throw new ArgumentException("Not exist column name: " + columnName + " or column is hidden");
			return GetColumnEditValue(column);

		}

		public object GetColumnEditValue(SpGvBaseColumn column) {
			Parse();
			if (column == null) throw new ArgumentNullException("column");
			return column.InternalGetValuePostedFromClientOnEditMode();
		}

		internal SpGridView Grid;


	}
}
